﻿package pages
{
	import com.gaiaframework.templates.AbstractPage;
	import com.gaiaframework.events.*;
	import com.gaiaframework.debug.*;
	import com.gaiaframework.api.*;
	import com.google.analytics.AnalyticsTracker;
	import com.google.analytics.GATracker;
	import com.greensock.plugins.DropShadowFilterPlugin;
	import com.greensock.plugins.TweenPlugin;
	import flash.display.*;
	import flash.events.*;
	import com.greensock.TweenMax;
	import model.Model;
	import sound.SoundManager;
	import ui.map.Credits;
	import ui.map.CustomMap;
	import ui.map.GoogleCredits;
	import ui.map.ZoomerMeasure;
	import utils.TextFieldUtil;
	
	public class HomePage extends AbstractPage
	{	
		private var _customMap:CustomMap;
		private var _googleCredits:GoogleCredits;
		private var _credits:Credits;
		private var _ambiantSound:SoundManager;
		private var _zoomer:ZoomerMeasure;
		private var _tracker:AnalyticsTracker;

		public function HomePage()
		{
			super();
			alpha = 0;
			//new Scaffold(this);
		}
		override public function transitionIn():void 
		{
			super.transitionIn();
			TweenPlugin.activate([DropShadowFilterPlugin]);
			TextFieldUtil.setStyle(IStyleSheet(Gaia.api.getPage(Pages.INDEX).assets.styles).style);
			addCustomMap();
			addCredits();
			addZoomer();
			_ambiantSound = new SoundManager();
			_onResize();
			stage.addEventListener(Event.RESIZE, _onResize);
			_tracker = new GATracker(this, Model.instance.getGa(), "AS3", false);
			_tracker.trackPageview('/home');

		}
		
		private function addZoomer():void 
		{
			_zoomer = new ZoomerMeasure();
			addChild(_zoomer)
		}
		
		private function addCredits():void 
		{
			_googleCredits = new GoogleCredits();
			addChild(_googleCredits);
			
			_credits = new Credits();
			addChild(_credits);
		}
		
		public function zoomMap(value:int):void
		{
			_customMap.zoomMap(value);
		}
		private function _onResize(e:Event=null):void 
		{
			var sw:Number = stage.stageWidth;
			if (_customMap)_customMap.resize();
			if (_googleCredits)
			{
				_googleCredits.x = sw - (_googleCredits.width + 26);
				_googleCredits.y = Gaia.api.getPage(Pages.NAV).content.footer.y - (_googleCredits.height+20);
			}
			if (_credits)
			{
				_credits.x = int((_googleCredits.x + _googleCredits.width) - (_credits.width-30));
				_credits.y = int(Gaia.api.getPage(Pages.NAV).content.footer.y-40);
			}
			if (_zoomer)
			{
				_zoomer.x = _zoomer.y = 33;
			}
		}
		
		private function addCustomMap():void 
		{
			_customMap = new CustomMap();
			_customMap.name = "_customMap";
			addChild(_customMap);
			TweenMax.to(this, 0.3, {delay:1, alpha:1, onComplete:transitionInComplete } );
			trace("[HomePage.addCustomMap] ",Model.instance.mapBackground);
		}
		override public function transitionOut():void 
		{
			super.transitionOut();
			_ambiantSound.destroy();
			stage.removeEventListener(Event.RESIZE, _onResize);
			TweenMax.to(this, 0.3, { alpha:0, onComplete:function():void { _customMap.destroy(); transitionOutComplete(); }} );
		}

	}
}
